package br.com.lucasddaniel.inbuzios.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import br.com.lucasddaniel.inbuzios.R;
import br.com.lucasddaniel.inbuzios.bean.TuristaBean;

public class TuristaSelecionadoAdapter extends ArrayAdapter<TuristaBean> {
	private Context context = null;
	private List<TuristaBean> allItems = null;
	private List<TuristaBean> items;
	private MyFilter filter;

	public TuristaSelecionadoAdapter(Context context, int resource,
			List<TuristaBean> listTuristasBean) {
		super(context, resource, listTuristasBean);
		this.context = context;
		this.items = listTuristasBean;
		this.allItems = this.items;
	}

	class MyFilter extends Filter {

		private TuristaSelecionadoAdapter adapter;

		public MyFilter(TuristaSelecionadoAdapter adapter) {
			this.adapter = adapter;
		}

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();

			if (constraint.toString().equals("")) {
				results.values = adapter.getAllItems();
				results.count = adapter.getAllItems().size();
				return results;
			}
			List<TuristaBean> filteredItems = new ArrayList<TuristaBean>();
			for (TuristaBean o : adapter.getAllItems()) {
				if (o.getNome().toUpperCase()
						.contains(constraint.toString().toUpperCase())) {
					filteredItems.add(o);
				}
			}

			results.values = filteredItems;
			results.count = filteredItems.size();
			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			adapter.setItems((List<TuristaBean>) results.values);
			adapter.notifyDataSetChanged();
		}
	}

	public List<TuristaBean> getItems() {
		return items;
	}

	public void setItems(List<TuristaBean> items) {
		this.items = items;
	}

	public List<TuristaBean> getAllItems() {
		return allItems;
	}

	public void setAllItems(List<TuristaBean> listTuristasBean) {
		this.items = listTuristasBean;
		this.allItems = this.items;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Filter getFilter() {
		if (this.filter == null)
			this.filter = new MyFilter(this);
		return this.filter;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(
				br.com.lucasddaniel.inbuzios.R.layout.row_turista_selecionado,
				parent, false);
		if (items != null && items.size() != 0) {
			TuristaBean turistasBean = items.get(position);
			if (turistasBean != null) {
				TextView txtViewNome = (TextView) rowView
						.findViewById(R.id_row_turista_selecionado.turistas_nome);
				TextView txtViewVooIn = (TextView) rowView
						.findViewById(R.id_row_turista_selecionado.turistas_voo_in);
				TextView txtViewPousada = (TextView) rowView
						.findViewById(R.id_row_turista_selecionado.turistas_pousada);
				ImageView imgChecked = (ImageView) rowView
						.findViewById(R.id_row_turista_selecionado.img);
				LinearLayout ll = (LinearLayout) rowView
						.findViewById(R.id_row_turista_selecionado.ll);
				ll.setBackgroundColor(this.context.getResources().getColor(
						R.color.bg_default));
				if (position % 2 == 0) {
					ll.setBackgroundColor(this.context.getResources().getColor(
							android.R.color.white));
				}

				turistasBean.setImg(imgChecked);
				txtViewNome.setText(turistasBean.getNome());
				txtViewVooIn.setText(turistasBean.getVooIn());
				txtViewPousada.setText(turistasBean.getPousada());
				if (turistasBean.isChecked()) {
					turistasBean.getImg().setVisibility(View.VISIBLE);
				} else {
					turistasBean.getImg().setVisibility(View.INVISIBLE);
				}
			}
		}
		return rowView;
	}
}
